package com.grape.service.take_delivery.impl;

import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grape.dao.take_delivery.WaybillRepository;
import com.grape.domain.take_delivery.WayBill;
import com.grape.elasticsearch.WaybillES;
import com.grape.service.take_delivery.WaybillService;

@Service
@Transactional
public class WaybillServiceImpl implements WaybillService{
	@Autowired
	private WaybillRepository wr;
	@Autowired
	private WaybillES we;

	@Override
	public String save(WayBill model) {
		// 没有订单的运单
		if (model.getOrder()!=null && model.getOrder().getId()==null) {
			model.setSignStatus(1);
			model.setOrder(null);
		}
		
		// 这张运单是否之前快速录入过,没有直接保存
		WayBill wb = wr.findByWayBillNum(model.getWayBillNum());
		if (wb == null) {
			wr.save(model);
			we.save(model);
		}
		
		// 之前有快速录入并且是代发货状态,将id赋予详细运单执行update操作
		if (wb != null ) {
			if (wb.getSignStatus()!=null && wb.getSignStatus()!=1) {
				return "保存失败，运单号为:"+model.getWayBillNum()+"的运单已发出";
			}
			
			model.setId(wb.getId());
			model.setSignStatus(1);
			wr.save(model);
			we.save(model);
		}
		
		return "保存成功,运单号为: " + model.getWayBillNum();
		
		// 老师在这里使用了对象属性拷贝的方法很有用但思路错了
		
	}
	@Override
	public Page<WayBill> pageQuery(Pageable pageable,WayBill wb) {
		if (	StringUtils.isBlank(wb.getWayBillNum()) 
				&& StringUtils.isBlank(wb.getRecAddress()) 
				&& StringUtils.isBlank(wb.getSendAddress()) 
				&& StringUtils.isBlank(wb.getSendProNum())
				&& wb.getSignStatus() == null) {
			System.out.println("从数据库查询");
			// 没有数据直接无条件查询
			return wr.findAll(pageable);
		} else {
			System.out.println("从ec查询");
			// 多条件组合查询,条件容器
			BoolQueryBuilder bqb = new BoolQueryBuilder();
			
			// 查询运单号
			if (StringUtils.isNotBlank(wb.getWayBillNum())) {
				System.out.println(wb.getWayBillNum());
				QueryBuilder tj1 = new TermQueryBuilder("wayBillNum",wb.getWayBillNum());
				bqb.must(tj1);
			}
			
			// 模糊查询,收发地址
			if (StringUtils.isNotBlank(wb.getRecAddress())) {
				QueryBuilder tj2 = new WildcardQueryBuilder("recAddress","*"+wb.getRecAddress()+"*");
				bqb.must(tj2);
			}
			if (StringUtils.isNotBlank(wb.getSendAddress())) {
				QueryBuilder tj3 = new WildcardQueryBuilder("sendAddress","*"+wb.getSendAddress()+"*");
				bqb.must(tj3);
			}
			System.out.println(wb.getSignStatus());
			
			// 速运类型 ,当日，次日，隔日
			if (StringUtils.isNotBlank(wb.getSendProNum())) {
				QueryBuilder tj4 = new TermQueryBuilder("sendProNum",wb.getSendProNum());
				bqb.must(tj4);
			}
			
			// 运单状态： 待发货、派送中、已签收、异常
			if (wb.getSignStatus()!=null  && wb.getSignStatus()!=0) {
				QueryBuilder tb4 = new TermQueryBuilder("signStatus",wb.getSignStatus());
				bqb.must(tb4);
			}
			SearchQuery sq = new NativeSearchQuery(bqb);
			sq.setPageable(pageable);
			
			
			return we.search(sq);
		}
		
	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		return wr.findByWayBillNum(wayBillNum);
	}
	
	
	
	
}
